#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QMessageBox>
#include <fstream>
#include <ctime>
#include <cmath>

using namespace std;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    this->SetMenuSize(GEAR);
    this->gl = new GameLayout(this);
    this->gp = new Graph(gl->arr,gl->lab);
    this->op = new Operate(gp);

    char str[10];
    sprintf(str,"%d",this->gl->score[0]->score);
    this->gl->score[0]->setText(str);
    sprintf(str,"%d",this->gl->score[1]->score);
    this->gl->score[1]->setText(str);

    connect(gl->pb[0],SIGNAL(clicked()),this,SLOT(btn_new_game()));
    connect(gl->pb[1],SIGNAL(clicked()),this,SLOT(btn_close_game()));

    ui->setupUi(this);

}

void Widget::SetMenuSize(uint gear)
{
    switch(gear)
    {
    case 1:     this->setMinimumSize(GW1,GH1);
                this->setMaximumSize(GW1,GH1);
                break;
    case 2:     this->setMinimumSize(GW2,GH2);
                this->setMaximumSize(GW2,GH2);
                break;
    case 3:     this->setMinimumSize(GW3,GH3);
                this->setMaximumSize(GW3,GH3);
                break;
    case 4:     this->setMinimumSize(GW4,GH4);
                this->setMaximumSize(GW4,GH4);
                break;
    default:    break;
    }
}

//判定游戏是否结束
void Widget::isgameover(int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    for(int j = 0;j < LabelY;j++)
    {
        if(2 == *arr[0][j])
        {
            switch(QMessageBox::information(this,"俄罗斯方块","游戏结束！再来一局吧？",QMessageBox::Yes,QMessageBox::No))
            {
                case QMessageBox::Yes:  gl->GameLayoutrebuild();
                                        this->btn_new_game();
                                        break;

                case QMessageBox::No:   this->btn_close_game();
                                        break;
            }
            return;
        }
    }
}

void Widget::bestscoreup(void)
{
    char str[10];
    if(this->gl->score[0]->score > this->gl->score[1]->score)
    {
        this->gl->score[1]->score = this->gl->score[0]->score;
        sprintf(str,"%d",this->gl->score[1]->score);
        this->gl->score[1]->setText(str);
    }
}

void Widget::keyPressEvent(QKeyEvent* event)
{
    switch(event->key())
    {
        case Qt::Key_Up:        op->uGraph(this->gp->border,this->gp->arr,this->gp->lab);
                                break;
        case Qt::Key_Down:      op->dGraph(this->gp->border,this->gp->arr,this->gp->lab);
                                break;
        case Qt::Key_Left:      op->lGraph(this->gp->border,this->gp->arr,this->gp->lab);
                                break;
        case Qt::Key_Right:     op->rGraph(this->gp->border,this->gp->arr,this->gp->lab);
                                break;
    }
}

void Widget::timerEvent(QTimerEvent* event)
{
    this->isgameover(this->gp->arr,this->gp->lab);
    this->bestscoreup();
    while(false == gp->IsMove)
        this->gp->GraphBulid(&(this->gp->PosX),&(this->gp->PosY));

    this->op->remove(this->gl->score[0],this->gp->arr,this->gp->lab);
    op->dGraph(this->gp->border,this->gp->arr,this->gp->lab);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btn_new_game(void)
{
    char str[10];
    this->gl->score[0]->score = 0;
    sprintf(str,"%d",this->gl->score[0]->score);
    this->gl->score[0]->setText(str);

    this->gp->GraphBulid(&(this->gp->PosX),&(this->gp->PosY));
    this->time_id = this->startTimer(1500);
    this->setFocus();
}

void Widget::btn_close_game(void)
{
    this->~Widget();
}

//游戏界面布局
GameLayout::GameLayout(QWidget* parent)
{
    this->GL = new QGridLayout(parent);
    this->gl1 = new QGridLayout;
    this->gl2 = new QGridLayout;
    gl1->setHorizontalSpacing(0);
    gl1->setVerticalSpacing(0);
    gl2->setHorizontalSpacing(50);
    gl2->setVerticalSpacing(20);
    GL->addLayout(gl1,0,0);
    GL->addLayout(gl2,0,1);

    for(int i = 0;i < LabelX;i++)
    {
        for(int j = 0;j < LabelY;j++)
        {
            this->arr[i][j] = new int;
            *arr[i][j] = 0;
            this->lab[i][j] = new GLabel(parent);
            gl1->addWidget(lab[i][j],i,j);
        }
    }

    for(int i = 0;i < 2;i++)
    {
        this->title[i] = new TLabel(i,parent);
        this->score[i] = new SLabel(parent);
        this->score[i]->score = 0;
        this->pb[i] = new PushButton(i,parent);
        gl2->addWidget(title[i],0,i,Qt::AlignBottom);
        gl2->addWidget(score[i],1,i,Qt::AlignTop);
        gl2->addWidget(pb[i],2,i,Qt::AlignVCenter|Qt::AlignHCenter);
    }
}

void GameLayout::GameLayoutrebuild(void)
{
    char color[256];
    for(int i = 0;i < LabelX;i++)
    {
        for(int j = 0; j < LabelY;j++)
        {
            *arr[i][j] = 0;
            lab[i][j]->type = 7;
            lab[i][j]->getGLabelcolor(color);
            lab[i][j]->setStyleSheet(color);
        }
    }
}

GameLayout::~GameLayout()
{
    delete GL;
    delete gl1;
    delete gl2;
    for(int i = 0;i < LabelX;i++)
    {
        for(int j = 0;j < LabelY;j++)
        {
            delete arr[i][j];
            delete lab[i][j];
        }
    }
    for(int i = 0;i < 2;i++)
    {
        delete title[i];
        delete score[i];
        delete pb[i];
    }
}

//图形边界
Border::Border(){}

Border::reBorder(int ub,int db,int lb,int rb)
{
    this->uBorder = ub;
    this->dBorder = db;
    this->lBorder = lb;
    this->rBorder = rb;
}

Border::~Border(){}

//图形生成
Graph::Graph(int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    this->PosX = 0;
    this->PosY = 0;
    this->IsMove = false;
    this->color = new char[256];
    this->border = new Border;
    for(int i = 0;i < LabelX;i++)
    {
         for(int j = 0;j < LabelY;j++)
         {
             this->arr[i][j] = arr[i][j];
             this->lab[i][j] = lab[i][j];
         }
    }
}

void Graph::GraphBulid(int* x,int* y)
{
    if(false == this->IsMove)
    {
        srand(time(NULL));
        switch(0)//rand()%7)
        {
            case 0:     this->SGraph(x,y);
                        break;
            case 1:     this->ZGraph(x,y);
                        break;
            case 2:     this->JGraph(x,y);
                        break;
            case 3:     this->LGraph(x,y);
                        break;
            case 4:     this->TGraph(x,y);
                        break;
            case 5:     this->IGraph(x,y);
                        break;
            case 6:     this->OGraph(x,y);
                        break;
        }
    }
    this->IsMove = true;
}

void Graph::SGraph(int* x,int* y)
{
    *x = 0;
    *y = LabelY/2-1;
    this->lab[0][LabelY/2-1]->type = 0;
    this->lab[0][LabelY/2]->type = 0;
    this->lab[1][LabelY/2-1]->type = 0;
    this->lab[1][LabelY/2-2]->type = 0;
    this->lab[0][LabelY/2-1]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[0][LabelY/2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-2]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[0][LabelY/2]) = 1;
    *(this->arr[1][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2-2]) = 1;
    this->border->reBorder(0,1,LabelY/2-2,LabelY/2);
}

void Graph::ZGraph(int* x,int* y)
{
    *x = 0;
    *y = LabelY/2;
    this->lab[0][LabelY/2-1]->type = 1;
    this->lab[0][LabelY/2]->type = 1;
    this->lab[1][LabelY/2]->type = 1;
    this->lab[1][LabelY/2+1]->type = 1; 
    this->lab[0][LabelY/2-1]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[0][LabelY/2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2+1]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[0][LabelY/2]) = 1;
    *(this->arr[1][LabelY/2]) = 1;
    *(this->arr[1][LabelY/2+1]) = 1;
    this->border->reBorder(0,1,LabelY/2-1,LabelY/2+1);
}

void Graph::JGraph(int* x,int* y)
{
    *x = 1;
    *y = LabelY/2;
    this->lab[0][LabelY/2]->type = 2;
    this->lab[1][LabelY/2]->type = 2;
    this->lab[2][LabelY/2]->type = 2;
    this->lab[2][LabelY/2-1]->type = 2;
    this->lab[0][LabelY/2]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2]->setStyleSheet(this->color);
    this->lab[2][LabelY/2]->setStyleSheet(this->color);
    this->lab[2][LabelY/2-1]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2]) = 1;
    *(this->arr[1][LabelY/2]) = 1;
    *(this->arr[2][LabelY/2]) = 1;
    *(this->arr[2][LabelY/2-1]) = 1;
    this->border->reBorder(0,2,LabelY/2-1,LabelY/2);
}

void Graph::LGraph(int* x,int* y)
{
    *x = 1;
    *y = LabelY/2-1;
    this->lab[0][LabelY/2-1]->type = 3;
    this->lab[1][LabelY/2-1]->type = 3;
    this->lab[2][LabelY/2-1]->type = 3;
    this->lab[2][LabelY/2]->type = 3;
    this->lab[0][LabelY/2-1]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[2][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[2][LabelY/2]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2-1]) = 1;
    *(this->arr[2][LabelY/2-1]) = 1;
    *(this->arr[2][LabelY/2]) = 1;
    this->border->reBorder(0,2,LabelY/2-1,LabelY/2);
}

void Graph::TGraph(int* x,int* y)
{
    *x = 1;
    *y = LabelY/2-1;
    this->lab[0][LabelY/2-1]->type = 4;
    this->lab[1][LabelY/2-1]->type = 4;
    this->lab[1][LabelY/2-2]->type = 4;
    this->lab[1][LabelY/2]->type = 4;
    this->lab[0][LabelY/2-1]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2-2]) = 1;
    *(this->arr[1][LabelY/2]) = 1;
    this->border->reBorder(0,1,LabelY/2-2,LabelY/2);
}

void Graph::IGraph(int* x,int* y)
{
    *x = 1;
    *y = LabelY/2-1;
    this->lab[0][LabelY/2-1]->type = 5;
    this->lab[1][LabelY/2-1]->type = 5;
    this->lab[2][LabelY/2-1]->type = 5;
    this->lab[3][LabelY/2-1]->type = 5;
    this->lab[0][LabelY/2-1]->getGLabelcolor(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[2][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[3][LabelY/2-1]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2-1]) = 1;
    *(this->arr[2][LabelY/2-1]) = 1;
    *(this->arr[3][LabelY/2-1]) = 1;
    this->border->reBorder(0,3,LabelY/2-1,LabelY/2-1);
}

void Graph::OGraph(int* x,int* y)
{
    *x = 0;
    *y = LabelY/2;
    this->lab[0][LabelY/2]->type = 6;
    this->lab[0][LabelY/2-1]->type = 6;
    this->lab[1][LabelY/2]->type = 6;
    this->lab[1][LabelY/2-1]->type = 6;
    this->lab[0][LabelY/2]->getGLabelcolor(color);
    this->lab[0][LabelY/2]->setStyleSheet(this->color);
    this->lab[0][LabelY/2-1]->setStyleSheet(this->color);
    this->lab[1][LabelY/2]->setStyleSheet(this->color);
    this->lab[1][LabelY/2-1]->setStyleSheet(this->color);
    *(this->arr[0][LabelY/2]) = 1;
    *(this->arr[0][LabelY/2-1]) = 1;
    *(this->arr[1][LabelY/2]) = 1;
    *(this->arr[1][LabelY/2-1]) = 1;
    this->border->reBorder(0,1,LabelY/2-1,LabelY/2);
}

Graph::~Graph()
{
    delete border;
    delete[] color;
}

//操坐
Operate::Operate(Graph* graph)
{
    this->graph = graph;
}

//方块旋转
void Operate::uGraph(Border* border,int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    if(6 == lab[this->graph->PosX][this->graph->PosY]->type)
        return;

    int NPosX = border->uBorder+(border->rBorder-this->graph->PosY);
    int NPosY = border->lBorder+(this->graph->PosX-border->uBorder);

    if(LabelX-1 < border->uBorder+border->rBorder-border->lBorder+this->graph->PosX-NPosX ||
       0 > border->uBorder+this->graph->PosX-NPosX ||
       LabelY-1 < border->lBorder+border->dBorder-border->uBorder+this->graph->PosX-NPosX ||
       0 > border->lBorder+this->graph->PosX-NPosX)
        return;

    int flag = 1;
    int temp1[LabelX][LabelY];
    int temp2[LabelX][LabelY];
    //int temp3[LabelX][LabelY];
    //int temp4[LabelX][LabelY];
    for(int i = border->uBorder;i < border->uBorder+border->rBorder-border->lBorder+1 && 1 == flag;i++)
    {
        for(int j = border->lBorder;j < border->lBorder+border->dBorder-border->uBorder+1 && 1 == flag;j++)
        {
            int px = border->uBorder+j-border->lBorder;
            int py = border->rBorder-(i-border->uBorder);
            if(2 == *arr[px][py])
            {
                if(1 == temp1[px][py])
                {
                    /*
                    flag = 0;
                    break;
                    */
                    return;
                }
                else
                {
                    temp1[px][py] = *arr[px][py];
                    temp2[px][py] = lab[px][py]->type;
                    temp1[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = 0;
                    temp2[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = 7;
                }
            }
            else if(2 == temp1[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY])
            {
                if(1 == *arr[px][py])
                {
                    /*
                    flag = 0;
                    break;
                    */
                    return;
                }
                /*
                else
                {
                    temp1[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = *arr[px][py];
                    temp2[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = lab[px][py]->type;
                }
                */
            }
            else
            {
                temp1[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = *arr[px][py];
                temp2[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = lab[px][py]->type;
            }

        }
    }

    if(1 == flag)
    {
        for(int i = border->uBorder;i < border->uBorder+border->rBorder-border->lBorder+1;i++)
        {
            for(int j = border->lBorder;j < border->lBorder+border->dBorder-border->uBorder+1;j++)
            {
                int px = border->uBorder+j-border->lBorder;
                int py = border->rBorder-(i-border->uBorder);
                if(2 != *arr[px][py])
                {
                    *arr[px][py] = 0;
                    lab[px][py]->type = 7;
                    lab[px][py]->getGLabelcolor(this->graph->color);
                    lab[px][py]->setStyleSheet(this->graph->color);
                }
            }
        }

        for(int i = border->uBorder;i < border->uBorder+border->rBorder-border->lBorder+1;i++)
        {
            for(int j = border->lBorder;j < border->lBorder+border->dBorder-border->uBorder+1;j++)
            {
                *arr[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = temp1[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY];
                lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->type = temp2[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY];
                lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->getGLabelcolor(this->graph->color);
                lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->setStyleSheet(this->graph->color);
            }
        }

        int temp = border->dBorder;
        border->dBorder = border->uBorder+border->rBorder-border->lBorder+this->graph->PosX-NPosX;
        border->rBorder = border->lBorder+temp-border->uBorder+this->graph->PosY-NPosY;
        border->uBorder = border->uBorder+this->graph->PosX-NPosX;
        border->lBorder = border->lBorder+this->graph->PosY-NPosY;
    }

    /*
    double spinx;
    double spiny;
    for(int i = border->uBorder;i < border->dBorder+1 && 1 == flag;i++)
    {
        for(int j = border->lBorder;j < border->rBorder+1 && 1 == flag;j++)
        {
            spinx = (-sin(atan2(i-this->graph->PosX,j-this->graph->PosX)+PI/2)+this->graph->PosX);
            spiny = (cos(atan2(i-this->graph->PosX,j-this->graph->PosY)+PI/2)+this->graph->PosY);
            printf("%d %d %d %d %f %f\n",i,j,this->graph->PosX,this->graph->PosY,spinx,spiny);
            if(1 == *arr[i][j] && 2 == *arr[(int)spinx][(int )spiny])
                flag = 0;
        }
    }*/

    /*
    int temp1[LabelX][LabelY];
    int temp2[LabelX][LabelY];
    int temp3[LabelX][LabelY];
    int temp4[LabelX][LabelY];
    if(1 == flag)
    {
        for(int i = border->uBorder;i < border->uBorder+border->rBorder-border->lBorder+1;i++)
        {
            for(int j = border->lBorder;j < border->lBorder+border->dBorder-border->uBorder+1;j++)
            {
                int px = border->uBorder+j-border->lBorder;
                int py =  border->rBorder-(i-border->uBorder);
                if(2 != *arr[px][py])
                {
                    temp1[i][j] = *arr[px][py];
                    *arr[px][py] = 0;
                    temp2[i][j] = lab[px][py]->type;
                    lab[px][py]->type = 7;
                    lab[px][py]->getGLabelcolor(this->graph->color);
                    lab[px][py]->setStyleSheet(this->graph->color);
                }
                else
                {
                    temp3[px][py] = *arr[px][py];
                    *arr[px][py] = 0;
                    temp4[px][py] = lab[px][py]->type;
                    lab[px][py]->type = 7;
                    lab[px][py]->getGLabelcolor(this->graph->color);
                    lab[px][py]->setStyleSheet(this->graph->color);
                }
            }
        }

        int NPosX = border->uBorder+(border->rBorder-this->graph->PosY);
        int NPosY = border->lBorder+(this->graph->PosX-border->uBorder);
        for(int i = border->uBorder;i < border->uBorder+border->rBorder-border->lBorder+1;i++)
        {
            for(int j = border->lBorder;j < border->lBorder+border->dBorder-border->uBorder+1;j++)
            {
                int px = border->uBorder+j-border->lBorder;
                int py = border->rBorder-(i-border->uBorder);
                if(2 == temp3[px][py])
                {
                    *arr[px][py] = temp3[px][py];
                    lab[px][py]->type = temp4[px][py];
                    lab[px][py]->getGLabelcolor(this->graph->color);
                    lab[px][py]->setStyleSheet(this->graph->color);
                }
                else
                {
                    *arr[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY] = temp1[i][j];
                    lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->type = temp2[i][j];
                    lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->getGLabelcolor(this->graph->color);
                    lab[i+this->graph->PosX-NPosX][j+this->graph->PosY-NPosY]->setStyleSheet(this->graph->color);
                }
            }
        }
        int temp = border->dBorder;
        border->dBorder = border->uBorder+border->rBorder-border->lBorder+this->graph->PosX-NPosX;
        border->rBorder = border->lBorder+temp-border->uBorder+this->graph->PosY-NPosY;
        border->uBorder = border->uBorder+this->graph->PosX-NPosX;
        border->lBorder = border->lBorder+this->graph->PosY-NPosY;
    }*/
}

//方块下移
void Operate::dGraph(Border* border,int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    if(LabelX-1 <= border->dBorder)
    {
        for(int i = border->uBorder;i < border->dBorder+1;i++)
        {
            for(int j = border->lBorder;j < border->rBorder+1;j++)
            {
                if(1 == *arr[i][j])
                    *arr[i][j] = 2;
            }
        }
        this->graph->IsMove = false;
        return;
    }


    int flag = 1;

    for(int j = border->lBorder;j < border->rBorder+1 && 1 == flag;j++)
    {
        for(int i = border->uBorder;i < border->dBorder+1;i++)
        {
            if(1 == *arr[i][j] && 2 == *arr[i+1][j])
                flag = 0;
            else if(1 == *arr[i][j] && 0 == *arr[i+1][j])
                break;
        }
    }

    if(0 == flag)
    {
        for(int i = border->uBorder;i < border->dBorder+1;i++)
        {
            for(int j = border-> lBorder;j < border->rBorder+1;j++)
            {
                if(1 == *arr[i][j])
                    *arr[i][j] = 2;
            }
        }
        this->graph->IsMove = false;
    }

    if(1 == flag)
    {
        int temp;
        for(int i = border->dBorder;i > border->uBorder-1;i--)
        {
            for(int j = border-> lBorder;j < border->rBorder+1;j++)
            {
                if(0 != *arr[i][j] && 2!= *arr[i][j])
                {
                    temp = *arr[i+1][j];
                    *arr[i+1][j] = *arr[i][j];
                    *arr[i][j] = temp;

                    temp = lab[i+1][j]->type;
                    lab[i+1][j]->type = lab[i][j]->type;
                    lab[i][j]->type = temp;
                }

                lab[i+1][j]->getGLabelcolor(this->graph->color);
                lab[i+1][j]->setStyleSheet(this->graph->color);
                lab[i][j]->getGLabelcolor(this->graph->color);
                lab[i][j]->setStyleSheet(this->graph->color);
            }
        }
        this->graph->PosX = this->graph->PosX+1;
        border->reBorder(border->uBorder+1,border->dBorder+1,border->lBorder,border->rBorder);
    }
}

//方块左移
void Operate::lGraph(Border* border,int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    if(0 >= border->lBorder)
        return;

    int flag = 1;
    
    for(int i = border->uBorder;i < border->dBorder+1 && 1 == flag;i++)
    {
        for(int j = border->rBorder;j > border->lBorder-1;j--)
        {
            if(1 == *arr[i][j] && 2 == *arr[i][j-1])
                flag = 0;
            else if(1 == *arr[i][j] && 0 == *arr[i][j-1])
                break;
        }
    }

    if(1 == flag)
    {
        int temp;
        for(int j = border->lBorder;j < border->rBorder+1;j++)
        {
            for(int i = border->uBorder;i < border->dBorder+1;i++)
            {
                if(0 != *arr[i][j] && 2!= *arr[i][j])
                {
                    temp = *arr[i][j-1];
                    *arr[i][j-1] = *arr[i][j];
                    *arr[i][j] = temp;

                    temp = lab[i][j-1]->type;
                    lab[i][j-1]->type = lab[i][j]->type;
                    lab[i][j]->type = temp;
                }
                lab[i][j-1]->getGLabelcolor(this->graph->color);
                lab[i][j-1]->setStyleSheet(this->graph->color);
                lab[i][j]->getGLabelcolor(this->graph->color);
                lab[i][j]->setStyleSheet(this->graph->color);
            }
        }
        this->graph->PosY = this->graph->PosY-1;
        border->reBorder(border->uBorder,border->dBorder,border->lBorder-1,border->rBorder-1);
    }
}

//方块右移
void Operate::rGraph(Border* border,int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    if(LabelY-1 <= border->rBorder)
        return;

    int flag = 1;

    for(int i = border->uBorder;i < border->dBorder+1 && 1 == flag;i++)
    {
        for(int j = border->lBorder;j < border->rBorder+1;j++)
        {
            if(1 == *arr[i][j] && 2 == *arr[i][j+1])
                flag = 0;
            else if(1 == *arr[i][j] && 0 == *arr[i][j+1])
                break;
        }
    }

    if(1 == flag)
    {
        int temp;
        for(int j = border->rBorder;j > border->lBorder-1;j--)
        {
            for(int i = border->uBorder;i < border->dBorder+1;i++)
            {
                if(0 != *arr[i][j] && 2!= *arr[i][j])
                {
                    temp = *arr[i][j+1];
                    *arr[i][j+1] = *arr[i][j];
                    *arr[i][j] = temp;

                    temp = lab[i][j+1]->type;
                    lab[i][j+1]->type = lab[i][j]->type;
                    lab[i][j]->type = temp;
                }

                lab[i][j+1]->getGLabelcolor(this->graph->color);
                lab[i][j+1]->setStyleSheet(this->graph->color);
                lab[i][j]->getGLabelcolor(this->graph->color);
                lab[i][j]->setStyleSheet(this->graph->color);
            }
        }
        this->graph->PosY = this->graph->PosY+1;
        border->reBorder(border->uBorder,border->dBorder,border->lBorder+1,border->rBorder+1);
    }
}

//方块消除
void Operate::remove(SLabel* sl,int* arr[LabelX][LabelY],GLabel* lab[LabelX][LabelY])
{
    int flag = 1;
    for(int i = LabelX-1;i > 0;i--)
    {
        flag = 1;
        for(int j = 0;j < LabelY && 1 == flag;j++)
        {
            if(2 != *arr[i][j])
                flag = 0;
        }

        if(1 == flag)
        {
            for(int j = 0;j < LabelY;j++)
            {
                for(int k = i;k > 0;k--)
                {
                    if(1 != *arr[k-1][j])
                    {
                        *arr[k][j] = *arr[k-1][j];
                        lab[k][j]->type = lab[k-1][j]->type;
                        lab[k][j]->getGLabelcolor(this->graph->color);
                        lab[k][j]->setStyleSheet(this->graph->color);
                    }
                    else
                        break;
                }
            }
            char str[10];
            sl->score = sl->score+10;
            sprintf(str,"%d",sl->score);
            sl->setText(str);
        }
    }
}

Operate::~Operate(){}
